package suanfa;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;

public class minMutation {
    public int minMutation(String startGene, String endGene, String[] bank) {
        Set<String> vis=new HashSet<>();//用来标记已经遍历过的状态
        Set<String> hash=new HashSet<>();//用来标记bank里面的字符串
        for(String s:bank) hash.add(s);
        char[] change={'A','C','G','T'};
        if(startGene.equals(endGene)) return 0;
        if(!hash.contains(endGene)) return -1;
        int step=0;
        vis.add(startGene);
        Queue<String> q=new LinkedList<>();
        q.add(startGene);
        while(!q.isEmpty()){
            step++;
            int sz=q.size();
            while(sz--!=0){
                String t=q.poll();
                for(int i=0;i<8;i++){
                    char[] tmp=t.toCharArray();
                    for(int j=0;j<4;j++){
                        tmp[i]=change[j];
                        String next=new String(tmp);
                        if(hash.contains(next) && !vis.contains(next)){
                            if(next.equals(endGene)) return step;
                            q.add(next);
                            vis.add(next);
                        }
                    }
                }
            }
        }
        return -1;
    }
}
